package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.VipInfoModel;
import com.jp.knowledge.util.ImageLoadUtil;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/6/15.
 */

public class VipInfoAdapter extends RecycleVAdapter<VipInfoModel.PrivilegeListBean> implements View.OnClickListener {

	private int screenWidth;

	public VipInfoAdapter(Context context, @NonNull List<VipInfoModel.PrivilegeListBean> list) {
		super(context, list);
		screenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
	}

	@Override
	public int getLayoutId(int viewType) {
		return R.layout.item_vip_info_list;
	}

	@Override
	public void onBind(RecycleVHolder holder, int position) {
		LinearLayout itemView = holder.get(R.id.list_item);
		VipInfoModel.PrivilegeListBean data = getData(position);

		int itemCount = itemView.getChildCount();
		for(int i = itemCount; i < data.getList().size() + 1 - itemCount; i++)
			itemView.addView(View.inflate(mContext, R.layout.item_vip_info, null));
		for(int i = itemCount; i < itemCount - data.getList().size() - 1; i++)
			itemView.removeViewAt(0);

		int itemWidth = 0;
		if(itemView.getChildCount() <= 4)
			itemWidth = screenWidth/itemView.getChildCount();
		else
			itemWidth = (int)(screenWidth/3.5+0.5);

		setTag(itemView.getChildAt(0), position*10+0);
		setItemView(itemView.getChildAt(0), itemWidth, false, data.getTitle(), data.getColor(), data.getIcon());
		for(int i = 1; i < itemView.getChildCount(); i++) {
			VipInfoModel.PrivilegeListBean.ListBean listBean = data.getList().get(i-1);
			if(listBean == null)
				continue;
			View item = itemView.getChildAt(i);
			setTag(item, position*10+i);
			setItemView(item, itemWidth, listBean.getIsButtom() == 1,
					listBean.getInfo(), listBean.getColor(), listBean.getIcon());
		}
	}


	private void setTag(View view, int tag){
		TextView vipInfoTv = (TextView) view.findViewById(R.id.vip_info_tv);
		vipInfoTv.setTag(tag);
	}
	private void setItemView(View view, int width, boolean isButton, String text, String textColor, String icon){
		TextView vipInfoTv = (TextView) view.findViewById(R.id.vip_info_tv);
		ImageView vipInfoIv = (ImageView) view.findViewById(R.id.vip_info_iv);

		if(isButton){
			vipInfoTv.setBackground(mContext.getResources().getDrawable(R.drawable.my_rbg_blue_press));
			vipInfoTv.setTextColor(mContext.getResources().getColor(R.color.white));
			vipInfoTv.setPadding((int)mContext.getResources().getDimension(R.dimen.space_middle),
					(int)mContext.getResources().getDimension(R.dimen.space_min_super),
					(int)mContext.getResources().getDimension(R.dimen.space_middle),
					(int)mContext.getResources().getDimension(R.dimen.space_min_super));
			vipInfoTv.setText(text);
			vipInfoTv.setClickable(true);
			vipInfoTv.setOnClickListener(this);
			vipInfoIv.setVisibility(View.GONE);
			return;
		} else{
			vipInfoTv.setPadding(0, 0, 0, 0);
			vipInfoTv.setClickable(false);
			vipInfoTv.setBackground(null);
		}

		view.setLayoutParams(new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT));
		if(TextUtils.isEmpty(text)){
			vipInfoTv.setVisibility(View.GONE);
		} else{
			vipInfoTv.setVisibility(View.VISIBLE);
			vipInfoTv.setText(text);
			try{
				vipInfoTv.setTextColor(Color.parseColor(textColor));
			} catch (Exception e){
				vipInfoTv.setTextColor(mContext.getResources().getColor(R.color.font_gray));
			}
		}
		if(TextUtils.isEmpty(icon)){
			vipInfoIv.setVisibility(View.GONE);
		} else{
			vipInfoIv.setVisibility(View.VISIBLE);
			ImageLoadUtil.load(mContext, icon, vipInfoIv);
		}
	}

	@Override
	public void onClick(View v) {
		int tab = (int) v.getTag();
		if(onBtnClickListener != null)
			onBtnClickListener.onBtnClick(tab/10, tab%10);
	}

	private OnBtnClickListener onBtnClickListener;
	public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener){
		this.onBtnClickListener = onBtnClickListener;
	}
	public interface OnBtnClickListener{
		void onBtnClick(int row, int column);
	}
}
